/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aybolit.core.objects;

import aybolit.core.objects.dao.BedsDao;
import aybolit.core.objects.dao.DepartmentDao;
import aybolit.core.objects.dao.HealthingDao;
import aybolit.core.objects.dao.PacientDao;
import aybolit.core.objects.dao.impl.HealthingDaoImp;
import aybolit.core.objects.model.*;
import java.io.Serializable;
import java.util.*;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Masumov Emin
 */
public class PacientBean implements Serializable {

    PacientDao pacientDao;
    DepartmentDao departmentDao;
    BedsDao bedsDao;
    HealthingDao healthingDaoImp;
    private String depId;
    private Map<String, String> department = new HashMap<String, String>();
    private String name;
    private String secName;
    private String lastName;
    private String seriya;
    private Date dataRojdeniya;
    private String adres;
    private String contacts;
    private String work;
    private String job;
    private String holost;
    private String sex;
    private Patientes pacient;
    private String pacientID;
    private String fl = "0";
    private boolean pacientFirst = false;
    private boolean dnevnik = false;
    private boolean laborotor = false;
    private boolean plan = false;
    private boolean epikris = false;
    private boolean istoriya = false;
    private boolean obsledovanie = false;
    private boolean diagnos=false;

    public Map<String, String> getDep() {

        getOperator().getLogin();
        List<Departments> list = departmentDao.findAllDepartment();
        for (Iterator iter = list.iterator(); iter.hasNext();) {
            Departments dep = (Departments) iter.next();
            department.put(dep.getName(), String.valueOf(dep.getDepartmentId()));
        }
        return department;
    }

    public Patientes getP(String id) {
        if (id.equals("")) {
            id = "0";
        }
        return pacientDao.findPacient(id);
    }

    public Patientes getPatOper(String id) {
        if (id.equals("")) {
            id = "0";
        }
       HttpSession ses = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
      ses.setAttribute("pacientID", id);
        return pacientDao.findPacient(id);
    }

    public List<Patientes> getPacientsWithbyStatus(String statusID) {
        return pacientDao.findPacientWithStatus(statusID);
    }

    public Map<String, String> getDepartment() {
        return department;
    }

    public void setDepartment(Map<String, String> department) {
        this.department = department;
    }

    public String getDepId() {
        return depId;
    }

    public void setDepId(String depId) {
        this.depId = depId;
    }

    public void save() {
        Patientes p = new Patientes();
        p.setName(name);
        p.setAddress(adres);
        p.setBirthday(dataRojdeniya);
        p.setFamilyStatus(Integer.parseInt(holost));
        p.setLastName(lastName);
        p.setMale(Integer.parseInt(sex));
        p.setMidName(secName);
        p.setPassport(seriya);
        p.setPatientStatus(4);
        p.setProfession(job);
        p.setWorkplace(work);
        p.setDepartmantId(Integer.parseInt(depId));
        p.setHospitalId(getOperator().getHospitalId());
        p.setOperatorId(getOperator().getOperId());

        pacientDao.addPacient(p);
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Сохранение", "Пациент сохранён"));

    }

    private Operators getOperator() {
        FacesContext context = FacesContext.getCurrentInstance();
        Operators oper = (Operators) context.getExternalContext().getSessionMap().get("oper");
        return oper;
    }

    public String insertSession(String string) {

        return string;
    }

    public String getAdres() {
        return adres;
    }

    public void setAdres(String adres) {
        this.adres = adres;
    }

    public String getContacts() {
        return contacts;
    }

    public void setContacts(String contacts) {
        this.contacts = contacts;
    }

    public Date getDataRojdeniya() {
        return dataRojdeniya;
    }

    public void setDataRojdeniya(Date dataRojdeniya) {
        this.dataRojdeniya = dataRojdeniya;
    }

    public String getHolost() {
        return holost;
    }

    public void setHolost(String holost) {
        this.holost = holost;
    }

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSecName() {
        return secName;
    }

    public void setSecName(String secName) {
        this.secName = secName;
    }

    public String getSeriya() {
        return seriya;
    }

    public void setSeriya(String seriya) {
        this.seriya = seriya;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getWork() {
        return work;
    }

    public void setWork(String work) {
        this.work = work;
    }

    public void setDepartmentDao(DepartmentDao departmentDao) {
        this.departmentDao = departmentDao;
    }

    public void setPacientDao(PacientDao pacientDao) {
        this.pacientDao = pacientDao;
    }

    public Patientes getPacient() {
        return pacient;
    }

    public void setPacient(Patientes pacient) {
        this.pacient = pacient;
    }

    public void pacientName(String name, String id) {
        setName(name);
        setPacientID(id);
        setFl("1");
    }

    public String getPacientID() {
        return pacientID;
    }

    public void setPacientID(String pacientID) {
        this.pacientID = pacientID;
    }

    public BedsDao getBedsDao() {
        return bedsDao;
    }

    public void setBedsDao(BedsDao bedsDao) {
        this.bedsDao = bedsDao;
    }

    public void changePacientStatus(String bedID) {
        setFl("0");
        setName("");
        Patientes patientes = pacientDao.findPacient(pacientID);
        patientes.setPatientStatus(3);
        int pid=patientes.getPatientId();
        pacientDao.changePacientStatus(patientes);
        Healing hel=new Healing();
        hel.setPatientId(pid);
        hel.setStatusId(12);
        healthingDaoImp.save(hel);
        Beds beds = bedsDao.findBedsById(bedID);
        beds.setBedStatusId(12);
        beds.setPatientId(Integer.parseInt(getPacientID()));
        bedsDao.changeBedStatus(beds);

        
    }

    public String getFl() {
        return fl;
    }

    public void setFl(String fl) {
        this.fl = fl;
    }

    public List<Patientes> getPacientsWithStatus3() {
        return pacientDao.findPacientWithStatus("3");
    }

    public Patientes getPacientInfo() {
        return pacientDao.findPacient(pacientID);
    }

    public boolean getPacientFirst() {
        return pacientFirst;
    }

    public void setPacientFirst(boolean pacientFirst) {
        this.pacientFirst = pacientFirst;
    }

    public boolean isDnevnik() {
        return dnevnik;
    }

    public void setDnevnik(boolean dnevnik) {
        this.dnevnik = dnevnik;
    }

    public boolean isEpikris() {
        return epikris;
    }

    public void setEpikris(boolean epikris) {
        this.epikris = epikris;
    }

    public boolean isIstoriya() {
        return istoriya;
    }

    public void setIstoriya(boolean istoriya) {
        this.istoriya = istoriya;
    }

    public boolean isLaborotor() {
        return laborotor;
    }

    public void setLaborotor(boolean laborotor) {
        this.laborotor = laborotor;
    }

    public boolean isPlan() {
        return plan;
    }

    public void setPlan(boolean plan) {
        this.plan = plan;
    }

    public boolean isObsledovanie() {
        return obsledovanie;
    }

    public void setObsledovanie(boolean obsledovanie) {
        this.obsledovanie = obsledovanie;
    }

    public boolean isDiagnos() {
        return diagnos;
    }

    public void setDiagnos(boolean diagnos) {
        this.diagnos = diagnos;
    }

    public HealthingDao getHealthingDaoImp() {
        return healthingDaoImp;
    }

    public void setHealthingDaoImp(HealthingDao healthingDaoImp) {
        this.healthingDaoImp = healthingDaoImp;
    }

   

    public void setPF(boolean p, boolean dnevnik, boolean laborotor, boolean plan, boolean obsledovanie, boolean epikriz, boolean istoriya,boolean diag) {
        System.out.println("___________________________________________________________");
        setPacientFirst(p);
        setDnevnik(dnevnik);
        setLaborotor(laborotor);
        setPlan(plan);
        setObsledovanie(obsledovanie);
        setEpikris(epikriz);
        setIstoriya(istoriya);
        setDiagnos(diag);
    }
}
